<template>
	<view class="main">
		<view class="mains">
			<u-sticky>
				<view class="top_c">
					<view class="top">
						<view class="items">
							<view class="lefts">
								<text>活动</text>
							</view>
							<view class="rights">
								<text>{{activity_name}}</text>
							</view>
						</view>
						<view class="items">
							<view class="lefts">
								<text>代理</text>
							</view>
							<view class="rights" @click="agentShow = true">
								<text class="products">{{agent_name}}</text>
								<u-icon size="24" space="8px" color="#1296db" name="list"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</u-sticky>
			<view class="content">
				<view v-if="configData && configData.configuration && configData.configuration.length > 0"
					class="types">
					<view v-if="configData.configuration && configData.configuration.length > 0" class="lists standard">
						<view class="title">
							<text>活动政策</text>
						</view>
						<view class="list-item" v-for="(item,index) in configData.configuration" :key="index">
							<view class="title">
								<text>配置 - {{index + 1}}</text>
							</view>
							<view class="dayFrame">
								<view class="lefts">
									<input type="text" placeholder="开始天数" v-model="item.start_day" disabled="true">
								</view>
								<view class="centers">
									<text>~</text>
								</view>
								<view class="rights">
									<input type="text" placeholder="结束天数" v-model="item.end_day" disabled="true">
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									<text>星展业交易额</text>
								</view>
								<view class="rights">
									<input type="text" placeholder="输入星展业交易额" v-model="item.hzy_trade_money"
										disabled="true">
									<text>元</text>
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									<text>燎原交易额</text>
								</view>
								<view class="rights">
									<input type="text" placeholder="输入燎原交易额" v-model="item.ly_trade_money"
										disabled="true">
									<text>元</text>
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									奖励金额
								</view>
								<view class="rights">
									<input type="text" :placeholder="'0 ~ ' + item.max_money" v-model="item.money">
									<text>元</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<u-empty v-else text="未设置" icon="/static/Empty/data_null.png"></u-empty>
			</view>
			<view class="saveBtn" @click="saveData">
				<text>提交</text>
			</view>
		</view>
		<u-picker :show="agentShow" :columns="agentColumns" keyName="label" @confirm="agentConfirm"
			@cancel="agentShow = false"></u-picker>
	</view>
</template>

<script>
	import tabbarItem from '../../../uni_modules/uview-ui/libs/config/props/tabbarItem'

	export default {
		data() {
			return {
				agentShow: false,
				agentColumns: [],
				activity_id: 0,
				activity_name: '',
				agent_id: null,
				agent_name: '请选择代理',
				configData: {},
				settings: 1,
				is_true: true,
				title: '',
				data: [],
				settlement_state: 1,
				policyMsg: '确定要保存吗？',
			}
		},
		onLoad(e) {
			this.activity_id = e.id
			this.activity_name = e.name
			this.getAgentList()
		},
		methods: {
			// 选择代理
			agentConfirm(e) {
				this.agent_id = e.value[0].id
				this.agent_name = e.value[0].label
				this.agentShow = false
				this.getAgentActivityPolicy()
			},
			// 获取代理信息
			async getAgentList() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getAgentList'
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.agentColumns = [res.data]
				}
			},
			// 获取代理活动政策信息
			async getAgentActivityPolicy() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getAgentActivityPolicy',
					data: {
						agent_id: this.agent_id,
						activity_id: this.activity_id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.settings = res.data.settings
					this.configData = res.data.config
				}
			},
			// 保存政策
			saveData() {
				var _this = this
				if (this.agent_id == '' || this.agent_id == null) {
					uni.showToast({
						title: '请选择代理！',
						icon: 'none',
						duration: 1000
					})
					return false
				}
				for (var i in this.configData.configuration) {
					if (this.configData.configuration[i].money == '' || this.configData.configuration[i].money == null) {
						uni.showToast({
							title: '请输入【配置 - ' + (parseInt(i) + 1) + '】奖励金额！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
					if (this.configData.configuration[i].money * 100 > this.configData.configuration[i].max_money * 100 || this
						.configData.configuration[i].money * 100 < 0) {
						uni.showToast({
							title: '【配置 - ' + (parseInt(i) + 1) + '】奖励金额超出限制！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
				}
				uni.showModal({
					content: this.policyMsg,
					success: (res) => {
						if (res.confirm) {
							_this.saveDataFun()
						}
					}
				})
			},
			// 保存政策方法
			async saveDataFun() {
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp({
						url: 'v1/saveAgentActivityPolicy',
						data: {
							configData: this.configData,
							settings: this.settings,
							agent_id: this.agent_id,
							activity_id: this.activity_id
						}
					});
					uni.hideLoading()
					this.is_true = true
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
					if (res.code == 200) {
						setTimeout(function() {
							uni.navigateBack({});
						}, 1000)
					}
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F2F6FC;
	}

	input {
		font-size: 28rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		width: 200rpx;
		background-color: #DCDFE6;
	}

	.main {
		.mains {

			.top_c {
				padding: 30rpx;
				background-color: #F2F6FC;

				.top {
					padding: 0 30rpx;
					background-color: #ffffff;
					border-radius: 18rpx;
					box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

					.items {
						padding: 30rpx 0;
						display: flex;
						justify-content: space-between;
						border-bottom: 1px solid #EBEEF5;

						.lefts {}

						.rights {
							display: flex;
							align-items: center;

							.phone {
								padding-left: 16rpx;
							}

							.products {
								padding-right: 16rpx;
							}
						}
					}
				}

				.tags {
					margin-top: 30rpx;
				}
			}

			.content {
				padding: 0 30rpx;
				margin-bottom: 120rpx;

				.types {

					.lists {
						padding: 30rpx 30rpx 0 30rpx;
						background-color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
						margin-bottom: 30rpx;

						.title {
							text-align: center;
							padding-bottom: 30rpx;
							border-bottom: 1px solid #EBEEF5;
							font-weight: bold;
							display: flex;
							justify-content: space-between;
							align-items: center;

							.rights {
								display: flex;
								align-items: center;

								.setZore {
									padding: 4rpx 18rpx;
									font-size: 24rpx;
									background-color: #fa3534;
									border-radius: 8rpx;
									color: #ffffff;
									font-weight: normal;
									margin-right: 16rpx;
								}
							}
						}

						.items {
							display: flex;
							justify-content: space-between;
							align-items: center;
							padding: 20rpx 0;
							border-top: 1px solid #EBEEF5;

							.rights {
								display: flex;
								justify-content: flex-end;
								align-items: center;

								input {
									padding-right: 60rpx;
								}

								text {
									position: absolute;
									padding-right: 10rpx;
								}

								.flow_unit {
									padding-right: 80rpx;
								}
							}
						}

						.items-add {
							border-top: 1px solid #EBEEF5;
							padding: 20rpx 0rpx;
							color: #2979ff;

							.add-btn {
								display: flex;
								justify-content: center;
								align-items: center;
								text-align: center;

								text {
									padding-left: 10rpx;
								}
							}
						}

						.list-item {
							margin-top: 30rpx;
							border-radius: 10rpx;
							border: 1px dashed #c8c9cc;
							padding: 20rpx 20rpx 0 20rpx;

							.dayFrame {
								padding: 20rpx 0;
								display: flex;
								border-bottom: 1px solid #EBEEF5;
								align-items: center;
								text-align: center;

								.lefts {
									width: 40%;
								}

								.centers {
									width: 20%;
								}

								.rights {
									width: 40%;
									display: flex;
									justify-content: flex-end;
								}
							}
						}

						.list-item:last-child {
							margin-bottom: 30rpx;
						}
					}

					.standard {
						padding-bottom: 10rpx;
					}

					.lists-add {
						padding: 30rpx 0rpx;
						background-color: #2979ff;
						color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

						.add-btn {
							display: flex;
							justify-content: center;
							align-items: center;
							text-align: center;

							text {
								padding-left: 10rpx;
							}
						}
					}
				}
			}

			.saveBtn {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				text-align: center;
				background-color: #409EFF;
				color: #ffffff;
				padding: 20rpx 0;
			}
		}
	}
</style>